package youcheng.work.view_zhexian;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


public class LoginActivity extends Activity {

    private EditText usr_id, pwd_id;
    private Button bt_login, bt_register;
    private ImageView img_return;
    private MySqliteopenhelper mySqliteopenhelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        mySqliteopenhelper = new MySqliteopenhelper(this);
        find();

        TextView tvTitle = findViewById(R.id.tv_title);
        tvTitle.setText("请登录");

        img_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();/*返回上一个activity*/
            }
        });
        bt_login.setOnClickListener(v -> {
            String id = usr_id.getText().toString().trim();
            String pwd= pwd_id.getText().toString().trim();
            if(usr_id.length() == 0||pwd_id.length() == 0)
            {
                Toast.makeText(LoginActivity.this, "请输入用户名和密码", Toast.LENGTH_SHORT).show();
                return ;
            }

            if (mySqliteopenhelper.login(id, pwd))
            {
                Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                startActivity(new Intent(LoginActivity.this, SelActivity.class));//从当前activity跳到selactivity

            }else{
                Toast.makeText(LoginActivity.this, "用户名或密码错误，未注册请注册", Toast.LENGTH_SHORT).show();
            }
        });

        bt_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(LoginActivity.this, RegisterActivity.class));//从当前activity跳到selactivity
            }
        });

    }

    private void find() {
        usr_id =findViewById(R.id.usrid);
        pwd_id =findViewById(R.id.pwd);
        img_return = findViewById(R.id.imgv_return);
        bt_login = findViewById(R.id.bt_login);
        bt_register = findViewById(R.id.bt_register);

    }

}